<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/30
 * Time: 9:12
 */

namespace plugin\admin\app\logic\user;


use plugin\admin\app\logic\BaseLogic;
use plugin\admin\app\model\font\FontModel;
use plugin\admin\app\model\novel\NovelChapterModel;
use plugin\admin\app\model\novel\NovelChapterPartModel;
use plugin\admin\app\model\novel\NovelSpreadModel;
use plugin\admin\app\model\plan\BackgroundMusicModel;
use plugin\admin\app\model\plan\PromotionPlanModel;
use plugin\admin\app\model\tencent\TimbreModel;
use plugin\admin\app\model\user\UserNovelChapterPartModel;
use plugin\admin\app\model\user\UserPromotionPlanModel;
use support\Request;
use support\Response;

class PromotionPartsLogic extends BaseLogic
{
    protected static $instance;

    public function __construct()
    {
        $this->model = new UserNovelChapterPartModel();
    }

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function select(Request $request): Response
    {
        $field = $request->input('field');
        if($field==false){
            $request->inputSet('field','number');
            $request->inputSet('order','asc');
        }

        //$request->inputSet('isdel','0');
        return parent::select($request);
    }

    protected function formatNormal($items, $total): Response
    {
        $novelMap = $this->getListMapById(NovelSpreadModel::getInstance(),$items,'novel_sn','novel_sn');
        $cahpterMap = $this->getListMapById(NovelChapterModel::getInstance(),$items,'chapterid','id');
        $planMap = $this->getListMapById(PromotionPlanModel::getInstance(),$items,'planid','id');
        foreach($items as &$val){
            $val['title'] = isset($novelMap[$val['novel_sn']])?$novelMap[$val['novel_sn']]['title']:$val['novel_sn'];
            $val['chaptername'] = isset($cahpterMap[$val['chapterid']])?$cahpterMap[$val['chapterid']]['name']:$val['chapterid'];
            $val['planname'] = isset($planMap[$val['planid']])?$planMap[$val['planid']]['name']:$val['planid'];
            $val['subtitle'] = $val['subtitle'] ? json_decode($val['subtitle'], true) : [];

        }
        return parent::formatNormal($items, $total);
    }

}